const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    region: ['安徽省', '合肥市', '蜀山区'],
    imgList: [],
    formdata:'',
    modalName: null,
    textareaAValue: '',
    textareaBValue: '',
    isName:"",
    isTel:"",
    isEmail:"",
    isWechat:"",
    uploadImgs:null
  },
	onLoad(){

	},

  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 9, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],//相册、相机
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          });
         
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        //上传图片
        var imgList = [];
        for (var i = 0; i < this.data.imgList.length; i++) {
          wx.uploadFile({
            url: 'https://xkcloude.hfhouse.com/routineclient/upload_file',
            filePath: this.data.imgList[i],
            name: 'photo_url',    
            success: function (res) {
                  console.log('图片上传的返回');
                  var imgs = JSON.parse(res.data);            
                  imgList.push(imgs.data.album);
            }
          })
        }
        this.setData({
          uploadImgs: imgList
        })
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '系统提示',
      content: '确定要删除当前照片吗？',
      cancelText: '取消',
      confirmText: '确认',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },

  nameVal(e){
    this.setData({
      isName: e.detail.value
    })
  },

  telVal(e) {
    this.setData({
      isTel: e.detail.value
    })
  },

  emailVal(e) {
    this.setData({
      isEmail: e.detail.value
    })
  },

  wechatVal(e) {
    this.setData({
      isWechat: e.detail.value
    })
  },

  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },


  //提交注册
  Registerpost () {
    var openid = app.globalData.openid;
    var vm = this;
    var isImg = vm.data.uploadImgs;
    if (vm.data.isName == '' || vm.data.isTel == '' || vm.data.isEmail == '' || vm.data.isWechat == '' || vm.data.textareaBValue == ''){
      wx.showToast({
        title: '带 * 为必填',
        icon: 'none',
        duration: 2000
      });
      return;
    } else if (isImg == null) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
        duration: 2000
      });
      return;
    }else{
      var name = vm.data.isName;
      var mobile = vm.data.isTel;
      var email = vm.data.isEmail;
      var wechat = vm.data.isWechat;
      var album = vm.data.uploadImgs;
      var desc = vm.data.textareaBValue;
      //var openid = wx.getStorageSync('user').openid;
      var openid = app.globalData.openid;
      var address = vm.data.region[0] + ' ' + vm.data.region[1] + ' ' + vm.data.region[2] + ' ';
      app.core('post', 'broker_register', {
        'name': name,
        'mobile': mobile,
        'email': email,
        'wechat': wechat,
        'album': album,
        'desc': desc,
        'openid': openid,
        'address': address
      }, (res) => {
        console.log(res);
        if (res.resultCode == 400){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
        } else if (res.resultCode == 200){
          wx.showToast({
            title: '注册成功',
            icon: 'none',
            duration: 2000
          });
        }else{
          wx.showToast({
            title: '网络出错！',
            icon: 'none',
            duration: 2000
          });
        }
      },
        (err) => {
          console.log(err)
        }
      )

    }
    
  }
})